package com.slkj.shilixiaoyuanapp.ui.teacher.tool.OutWork;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.google.gson.Gson;
import com.lljjcoder.citypickerview.widget.CityPicker;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.JsonBean;
import com.slkj.shilixiaoyuanapp.entity.PeopleEntity;
import com.slkj.shilixiaoyuanapp.entity.code.ToolCode;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.ChosePeopleActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.common.CommonToolHistoryActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.util.GetJsonDataUtil;
import com.slkj.shilixiaoyuanapp.util.TimeUtils;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.PickerViewProvider;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;
import com.tkk.api.RxEventProcessor;
import com.tkk.api.annotation.Recipient;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.RxEventProcessorTag.CHOSE_SPR_FINSH;

@ActivityInfo(layout = R.layout.activity_aplyy_outwork, title = "出差", rightHint = "历史记录")
public class AplyyOutWorkActivity extends BaseActivity {
    @BindView(R.id.tv_start_time) TextView tvStartTime;
    @BindView(R.id.tv_end_time) TextView tvEndTime;
    @BindView(R.id.tv_spr) TextView tvSpr;
    @BindView(R.id.edit_day) TextView editDay;
    @BindView(R.id.edit_where) TextView editWhere;
    @BindView(R.id.edit_content) NumberEditText editContent;
    @BindView(R.id.tv_user_name) TextView tvUserName;
    @BindView(R.id.tv_now_day_time) TextView tvNowDayTime;
    List<Integer> sqrIds = new ArrayList<>();
    private ArrayList<PeopleEntity> peopleEntities;
    public static final String tag = "AplyyOutWorkActivity";
    private String startTime, endTime;
    Date date_start = null;
    Date date_end = null;

    @Override
    protected void init() {
        RxEventProcessor.get().bind(this);
        tvUserName.setText(UserRequest.getInstance().getUser().getUser().getUName());
        tvNowDayTime.setText(TimeUtils.getNowNyr());
    }
    @Override
    protected void onRightTitleClick() {
        CommonToolHistoryActivity__JumpCenter
                .builder(this)
                .setNetToolType(5)
                .setToolType(ToolCode.TOOL_CC)
                .create().go();
    }

    @OnClick(R.id.tv_up)
    void upData() {
        String posStr = editWhere.getText().toString();
        if (TextUtils.isEmpty(posStr)) {
            showToast("请输入出差地点");
            return;
        }
        if (TextUtils.isEmpty(startTime)) {
            showToast("请选择开始时间");
            return;
        }
        if (TextUtils.isEmpty(endTime)) {
            showToast("请选择结束时间");
            return;
        }
        String day = editDay.getText().toString();
        if (TextUtils.isEmpty(day)) {
            showToast("请输入出差时长");
            return;
        }
        String context = editContent.getText();
        if (TextUtils.isEmpty(context)) {
            showToast("请输入出差事由");
            return;
        }
        if (sqrIds.size() == 0) {
            showToast("请选择审批人");
            return;
        }


        HttpHeper.get().toolService()
                .addOutWork(posStr,sqrIds.toString(),startTime,endTime,day,context)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(AplyyOutWorkActivity.this,data);
                        clearContent();
                    }
                });
    }
    @Recipient(tag = tag+ CHOSE_SPR_FINSH)
    void onChoseObjFinsh(int code, String info, List<PeopleEntity> peopleEntityList, List<Integer> checkIds) {
        tvSpr.setText(checkIds.size() == 0 ? "" : info);
        sqrIds.clear();
        sqrIds.addAll(checkIds);
        peopleEntities.clear();
        peopleEntities.addAll(peopleEntityList);
    }

    @OnClick(R.id.layout_chose_type)
    public void onLayoutChoseCityClicked(){
        CityPicker cityPicker = new CityPicker.Builder(AplyyOutWorkActivity.this)
                .textSize(14)
                .title("")
                .titleBackgroundColor("#FFFFFF")
                .confirTextColor("#0087FC")
                .cancelTextColor("#696969")
                .province("四川省")
                .city("成都市")
                .district("武侯区")
                .textColor(Color.parseColor("#333333"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.show();
        //监听方法，获取选择结果
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                //省份
                String province = citySelected[0];
                //城市
                String city = citySelected[1];
                //区县（如果设定了两级联动，那么该项返回空）
                String district = citySelected[2];
                //邮编
                String code = citySelected[3];
                //为TextView赋值
                editWhere.setText(province.trim() + " " + city.trim() + " " + district.trim());
            }
        });
    }

    @OnClick(R.id.layout_chose_start_time)
    public void onLayoutChoseStartTimeClicked() {
        PickerViewProvider.getDayTimePickerBefor(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                date_start=date;
                startTime = TimeUtils.dateToStr(date);

                tvStartTime.setText(startTime);
                if (date_end!=null&&date_start!=null){
                    editDay.setText(TimeUtils.getGapCount(date_start,date_end)+"");
                }
            }
        },date_end).show();
    }

    @OnClick(R.id.layout_chose_end_time)
    public void onLayoutChoseEndTimeClicked() {
        PickerViewProvider.getDayTimePickerAfter(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                date_end=date;
                endTime = TimeUtils.dateToStr(date);

                tvEndTime.setText(endTime);
                if (date_end!=null&&date_start!=null){
                    editDay.setText(TimeUtils.getGapCount(date_start,date_end)+"");
                }
            }
        },date_start).show();
    }

    @OnClick(R.id.layout_chose_spr)
    public void onLayoutChoseSprClicked() {
        if (peopleEntities == null) {
            HttpHeper.get().toolService()
                    .getCcSqr()
                    .compose(bindToLifecycle())
                    .compose(getThread())
                    .subscribe(new CommonCallBack<ArrayList<PeopleEntity>>(true, this) {
                        @Override
                        public void onCallBackSuccess(ArrayList<PeopleEntity> data) {
                            peopleEntities = data;
                            ChosePeopleActivity__JumpCenter.builder(AplyyOutWorkActivity.this).setTag(tag).setData(peopleEntities).create().go();
                        }
                    });
        }else {
            ChosePeopleActivity__JumpCenter.builder(this).setTag(tag).setData(peopleEntities).create().go();

        }

    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxEventProcessor.get().unBind(this);
    }
}
